///********************************************************************
///* Copyright © 2011 fishr (fishr.flash@gmail.com)  
///********************************************************************
package su.fishr.effects.prismavision 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import su.fishr.effects.prismavision.PV_model;
	import su.fishr.effects.prismavision.views.PrismasHolder;
	
   /**
	 * ...
	 * @version                1.0
	 * @playerversion          Flash 9
	 * @langversion            3.0
	 * @author                 fishr
	 * @created                15.08.2011 18:28
	 * @since                  15.08.2011 18:28
	 */
	internal class PV_view extends Sprite
	{
	
		
	/**-------------------------------------------------------------------------------
	* 
	*	   						V A R I A B L E ' S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		static public const AUTHOR:String = "fishr (fishr.flash@gmail.com)";	
		static public const END_ROTATION_CYCLE:String = "endRotationCycle";
		private var _model:PV_model;
		
		private var _senseOfRotate:int;
		private var _vertical:Boolean;
		private var _asyn:Number;
		private var _blur:Number;
		private var _gradient:Number;
		private var _prevImgs:Array;
		private var _secImgs:Array;
		private var _replaceHolders:Boolean = false;
		private var _holder:PrismasHolder;
		private var _corrBrightness:Number = 1.8;
		private var _factorShadow:Number = 1.7;

	//}
	/**-------------------------------------------------------------------------------
	* 
	*	 						P R O P E R T I E S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		public function get senseOfRotate():int 
		{
			return _senseOfRotate;
		}
		
		public function set senseOfRotate(value:int):void 
		{
			_senseOfRotate = value;
		}
		
		public function get vertical():Boolean 
		{
			return _vertical;
		}
		
		public function set vertical(value:Boolean):void 
		{
			_vertical = value;
		}
		
		public function get asyn():Number 
		{
			return _asyn;
		}
		
		public function set asyn(value:Number):void 
		{
			_asyn = value;
		}
		
		public function get blur():Number 
		{
			return _blur;
		}
		
		public function set blur(value:Number):void 
		{
			_blur = value;
		}
		
		public function get gradient():Number 
		{
			return _gradient;
		}
		
		public function set gradient(value:Number):void 
		{
			_gradient = value;
		}
		
		public function set replaceHolders(value:Boolean):void 
		{
			_replaceHolders = value;
		}
		
		public function get replaceHolders():Boolean 
		{
			return _replaceHolders;
		}
		
		public function get corrBrightness():Number
		{
			return _corrBrightness;
		}
		
		public function set corrBrightness(value:Number):void 
		{
			_corrBrightness = value;
		}
		
		public function get factorShadow():Number 
		{
			return _factorShadow;
		}
		
		public function set factorShadow(value:Number):void 
		{
			_factorShadow = value;
		}
	//}
	/**-------------------------------------------------------------------------------
	* 
	*								P U B L I C 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		public function PV_view(model:PV_model)
		{
			super();
			_model = model;
			_model.addEventListener( PV_model.CREATE_DATAS, createDatasHandler);
			
			
		}
		
		public function destroyThis():void
		{
			_model.removeEventListener( PV_model.CREATE_DATAS, createDatasHandler);
			_holder.removeEventListener( PrismasHolder.END_TURN, endTurnHandler);
			while ( this.numChildren ) this.removeChildAt(0);
		}
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*								P R I V A T E 	
	* 
	* --------------------------------------------------------------------------------
	*/	
	//{
		
		private function createDatasHandler(e:Event):void 
		{

			var replace:PrismasHolder;
			if ( !_holder )
			{
				_holder = new PrismasHolder( _model.arrFragments, _model.arrMasksLaer, _corrBrightness, _factorShadow);
				_holder.addEventListener( PrismasHolder.END_TURN, endTurnHandler);
				this.addChild( _holder );
			}
			else if ( _replaceHolders === true )
			{
				replace = new PrismasHolder( _model.arrFragments, _model.arrMasksLaer, _corrBrightness, _factorShadow);
				this.addChild( replace );
				this.removeChild( _holder );
				_holder = null;
				_holder = replace;
				_replaceHolders = false;
				_model.addEventListener( PV_model.CREATE_DATAS, createDatasHandler);
			}
			else
			{
				_model.removeEventListener( PV_model.CREATE_DATAS, createDatasHandler);
				_holder.addEventListener( PrismasHolder.END_TURN, endTurnHandler);
				_holder.createSecond( _model.arrFragments, _model.fwdSteps, _model.backSteps, _asyn, _vertical, _senseOfRotate, _model.arrMasksLaer);
				
			}

		}
		
		private function endTurnHandler(e:Event):void 
		{
			_model.addEventListener( PV_model.CREATE_DATAS, createDatasHandler);
			_holder.removeEventListener( PrismasHolder.END_TURN, endTurnHandler);
			this.dispatchEvent( new Event( END_ROTATION_CYCLE ) );
		}

	//}

	}

}